<template>
  <div>
   <el-dialog
      :title="isAdd ? '轮播图管理' : '编辑轮播图' "
      :visible.sync="isShow"

    >
        <el-form  :model="bannerFrom"  ref="bannerRef">
        <el-form-item
          label="标题"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="bannerFrom.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  label="图片" :label-width="formLabelWidth">
          <el-upload
            :limit="1"
            :on-preview="onPreview"
            action="#"
            :file-list="fileList"
            list-type="picture-card"
            :on-change="changeInfo"
            :auto-upload="false"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible" :append-to-body="true">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerFrom.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { banneradd, bannerinfo, banneredit } from '../../../api/api.js'
export default {
  props: ['isShow', 'isAdd'],

  data () {
    return {
      formLabelWidth: '120px',
      bannerFrom: {
        title: '',
        img: '',
        status: 1
      },
      fileList: [],
      dialogVisible: false,
      dialogImageUrl: ''
    }
  },
  methods: {
    onPreview (file) {
      //   打开预览框
      this.dialogVisible = true
      // 给弹框赋值图片地址
      this.dialogImageUrl = file.url
    },
    changeInfo (file) {
      this.bannerFrom.img = file.raw
    },
    add () {
      this.$refs.bannerRef.validate((val) => {
        if (val) {
          if (this.isAdd) {
            banneradd(this.bannerFrom)
              .then((res) => {
                if (res.code === 200) {
                  this.$message.success(res.msg)
                  this.$store.dispatch('banner/getBanner')
                  this.cancel()
                } else {
                  this.$message.error(res.msg)
                }
              })
          } else {
            console.log(this.bannerFrom)
            // 编辑接口
            banneredit(this.bannerFrom).then((res) => {
              console.log(res)
              if (res.code === 200) {
                // 添加成功
                this.$message.success(res.msg)
                this.cancel()
                this.$store.dispatch('banner/getBanner')
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    reset () {
      this.bannerFrom = {
        title: '',
        img: '',
        status: 1
      }
      this.fileList = []
      this.$refs.bannerRef.resetFields()
    },
    cancel () {
      this.$emit('cancel', false)
      this.reset()
    },
    lookup (id) {
      // 调用一条数据查询
      bannerinfo({
        id
      }).then((res) => {
        if (res.code === 200) {
          this.bannerFrom = res.list
          this.bannerFrom.id = id
          // 图片的回显
          this.fileList = this.bannerFrom.img ? [{ url: this.bannerFrom.img }] : []
        }
      })
    }
  }

}
</script>

<style>

</style>
